// import initCalendar, {
//   setTodoLabels
// } from '../../template/calendar/index';

const {
  $util,
} = getApp();

Page({
  data: {
    calendar: {
      theme: {
        color: '#abcdef',
        headColor: '#ff8800',
        choosedColor: '#23aee6',
      },
    },
    signTimer: [
      [0, 1, 2, 3, 4, 5, 6],  
      [-1, 0, 1, 2, 3, 4, 5],  
      [-2, -1, 0, 1, 2, 3, 4],
      [-3, -2, -1, 0, 1, 2, 3],
      [-4, -3, -2, -1, 0, 1, 2],
    ],
    hasSigned: false,
    checkinList: [],
    continueDays: 1,
    imgUrl: $util.getImgUrl(),
  },
  onLoad(ops) {
    this.sign();
    this.signRule();
    wx.setNavigationBarTitle({
      title: '每日签到',
    });
  },
  getList(signTimer) {
    let that = this;
    let { checkinList = [] } = that.data;
    for (let i = 0; i < signTimer.length; i++) {
      checkinList[i] = that.getDay(signTimer[i]);
    }
    this.setData({
      checkinList,
    });
  },
  init() {
    // initCalendar({
    //   theme: {
    //     color: '#777',
    //     headColor: '#333',
    //     choosedColor: '#e43a37',
    //   },
    //   disablePastDay: true,
    //   whenChangeMonth(curt, next) {
    //     console.log(curt, next);
    //   },
    // });
  },
  sign() {
    let that = this;
    $util.request({
      url: 'entry/wxapp/sign.list.setsign',
      data: {},
      success: (res) => {
        if (res.code === 1) {
          wx.showToast({
            title: '签到成功',
            duration: 2e3,
          });
        }
        this.setData({
          hasSigned: true,
          totalSign: res.continuousdaysign,
          continuousdaysign: res.continuousdaysign > 4 ? 4 : res.continuousdaysign,
          signcredit: res.signcredit,
          isLoad: true,
        });
        let cc = res.continuousdaysign > 4 ? that.data.signTimer[3] : that.data.signTimer[res.continuousdaysign - 1];
        that.getList(cc);
      },
    });
  },
  signRule() {
    $util.request({
      url: 'entry/wxapp/sign.list.signrule',
      data: {},
      success: (res) => {
        this.setData({
          content: res.content,
        });
      },
    });
  },
  getDay(day) {
    let today = new Date();
    let milliSeconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(milliSeconds);

    let tMonth = today.getMonth();
    let tDate = today.getDate();
    tMonth = this.fmt(tMonth + 1);
    tDate = this.fmt(tDate);

    return tMonth + '.' + tDate;
  },

  fmt(month) {
    let m = month;
    if (month.toString().length == 1) {
      m = '0' + month;
    }
    return m;
  },
});
